package com.leslie.wukong.biz.product.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leslie.wukong.biz.product.client.HandlerClient;
import com.leslie.wukong.biz.product.config.ProductCheckHandlerConfig;
import com.leslie.wukong.biz.product.converter.ObjectConverter;
import com.leslie.wukong.biz.product.domain.BusProduct;
import com.leslie.wukong.biz.product.domain.vo.ProductVO;
import com.leslie.wukong.biz.product.handler.AbstractCheckHandler;
import com.leslie.wukong.biz.product.mapper.BusProductMapper;
import com.leslie.wukong.biz.product.service.BusProductService;
import com.leslie.wukong.common.core.domain.RestResult;
import com.leslie.wukong.common.core.uuid.UUID;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * ClassName: BusProductServiceImpl
 * Package: com.leslie.wukong.biz.product.service
 * Description:
 * @date: 2024/12/3 14:24
 * @author: itcoder
 */

@Service
@RequiredArgsConstructor
@Slf4j
public class BusProductServiceImpl extends ServiceImpl<BusProductMapper, BusProduct> implements BusProductService {

    private final Map<String, AbstractCheckHandler> handlerMap;

    @Override
    public int insertSelective(BusProduct record) {
        return baseMapper.insertSelective(record);
    }

    @Override
    public int updateByPrimaryKeySelective(BusProduct record) {
        return baseMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public RestResult<Integer> createProduct(ProductVO param) {
        RestResult<Integer> paramCheckResult = this.paramCheck(param);
        param.setSkuId(UUID.randomUUID(true).toString().replaceAll("-", ""));
        if (!paramCheckResult.isSuccess()) {
            return paramCheckResult;
        }
        BusProduct busProduct = ObjectConverter.INSTANCE.productVoToBusProduct(param);
        return this.save(busProduct) ? RestResult.ok(1) : RestResult.fail("商品创建 失败 ....");
    }

    /**
     * 参数校验：责任链模式
     * @param param
     * @return
     */
    private RestResult<Integer> paramCheck(ProductVO param) {
        ProductCheckHandlerConfig handlerConfig = this.getHandlerConfigFile();
        AbstractCheckHandler handler = this.getHandler(handlerConfig);
        // 责任链:执行处理器链路
        RestResult<Integer> executeChainResult = HandlerClient.executeChain(handler, param);
        if (!executeChainResult.isSuccess()) {
            log.info("创建商品 失败 ....");
            return executeChainResult;
        }
        return RestResult.ok(1);
    }

    private AbstractCheckHandler getHandler(ProductCheckHandlerConfig config) {
        if (ObjectUtil.isNull(config)) {
            return null;
        }
        // 配置错误
        String handler = config.getHandler();
        if (StrUtil.isBlank(handler)) {
            return null;
        }
        // 配置了不存在的处理器
        AbstractCheckHandler abstractCheckHandler = handlerMap.get(handler);
        if (ObjectUtil.isNull(abstractCheckHandler)) {
            return null;
        }
        abstractCheckHandler.setConfig(config);
        abstractCheckHandler.setNextHandler(this.getHandler(config.getNext()));
        return abstractCheckHandler;
    }

    private ProductCheckHandlerConfig getHandlerConfigFile() {
        //配置中心存储的配置
        String configJson = "{\"handler\":\"nullValueCheckHandler\",\"next\":{\"handler\":\"priceCheckHandler\",\"next\":{\"handler\":\"stockCheckHandler\",\"next\":null}}}";
        //转成Config对象
        return JSON.parseObject(configJson, ProductCheckHandlerConfig.class);

    }
}
